package com.atguigu.flink.window;

import com.atguigu.flink.bean.Dept;
import com.atguigu.flink.bean.Emp;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

public class IntervalJoin {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<Emp> empDS = env
                .socketTextStream("hadoop102", 8888)
                .map(
                        new MapFunction<String, Emp>() {
                            @Override
                            public Emp map(String line) throws Exception {
                                String[] s = line.split(" ");
                                return new Emp(Integer.valueOf(s[0]), s[1], Integer.valueOf(s[2]), Long.valueOf(s[3]));
                            }
                        }
                )
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<Emp>forMonotonousTimestamps()
                                .withTimestampAssigner((emp, l) -> emp.getTs()));
        SingleOutputStreamOperator<Dept> DeptDS = env
                .socketTextStream("hadoop102", 8888)
                .map(
                        new MapFunction<String, Dept>() {
                            @Override
                            public Dept map(String line) throws Exception {
                                String[] s = line.split(" ");
                                return new Dept(Integer.valueOf(s[0]), s[1], Long.valueOf(s[3]));
                            }
                        }
                )
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<Dept>forMonotonousTimestamps()
                                .withTimestampAssigner((dept, l) -> dept.getTs()));
        SingleOutputStreamOperator<Tuple2<Emp, Dept>> processDS = empDS
                .keyBy(Emp::getDeptno)
                .intervalJoin(DeptDS.keyBy(Dept::getDeptno))
                .between(Time.milliseconds(-5), Time.milliseconds(5))
                .process(
                        new ProcessJoinFunction<Emp, Dept, Tuple2<Emp, Dept>>() {
                            @Override
                            public void processElement(Emp emp, Dept dept, ProcessJoinFunction<Emp, Dept, Tuple2<Emp, Dept>>.Context context, Collector<Tuple2<Emp, Dept>> collector) throws Exception {
                                collector.collect(Tuple2.of(emp, dept));
                            }
                        }
                );
        processDS.print();
        env.execute();
    }
}
